<?php

namespace App\Services;

use App\Http\Resources\Backend\WechatGroupResource;
use App\Models\WechatGroup;
use Illuminate\Support\Facades\DB;

/**
 * Class WechatGroupService
 * @package App\Services
 */
class WechatGroupService
{
    /**
     * @param WechatGroup $wechatGroup
     * @return WechatGroupResource|string
     * @throws \Exception
     */
    public static function edit(WechatGroup $wechatGroup)
    {
        $wechatGroup->is_enable = $wechatGroup->is_enable == 1 ? $wechatGroup->is_enable = 0 : $wechatGroup->is_enable = 1;
        DB::beginTransaction();
        try {
            WechatGroup::where('is_enable', '=', 1)->update(['is_enable' => 0]);        // 停用全部活动
            $wechatGroup->save();                                                       // 更新状态
            DB::commit();
            return $wechatGroup;
        }catch (\Exception $exception) {
            DB::rollBack();
            return $exception->getMessage();
        }
    }
}